package ops;

import java.util.Locale;

import javax.swing.SwingUtilities;

import ops.controller.ControllerManager;
import ops.i18n.Resources;
import ops.model.manager.EntityController;
import ops.model.manager.EntityManager;

/**
 * This class holds a reference too all important components of the application.
 * It uses the Singleton pattern in order to make it world-wide accessible.
 */
public class Application {

	private static Application instance = null;

	private Resources resources = null;
	private EntityController entityController = null;
	private WindowManager windowManager;
	private ControllerManager controllerManager;
	
	private Application() {	
	}

	/**
	 * Unique way of accessing an instance of the {@link Application} class.
	 */
	public static Application getInstance() {
		if (instance == null) {
			instance = new Application();
			instance.init();
		}
		
		return instance;
	}
	
	private void init() {
		// TODO support language choice!
		//resources = new Resources(new Locale("pt","BR"));
		//resources = new Resources(new Locale("en","US"));
		resources = new Resources(Locale.getDefault());

		entityController = new EntityController();
		
		controllerManager = new ControllerManager();
		
		windowManager = new WindowManager();
		windowManager.init();
	}
	
	/**
	 * @return the {@link Resources} manager.
	 * @see Resources
	 */
	public Resources getResources() {
		return resources;
	}
	
	/**
	 * @return the {@link EntityManager}
	 * @see EntityManager 
	 */
	public EntityController getEntityController() {
		return entityController;
	}
	
	/**
	 * @return the {@link WindowManager}
	 */
	public WindowManager getWindowManager() {
		return windowManager;
	}
	
	/**
	 * @return {@link ControllerManager}
	 */
	public ControllerManager getControllerManager() {
		return controllerManager;
	}
	
	
	public static void main(String[] args) {				
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Application.getInstance();
				
				WindowManager.getInstance().getApplicationFrame().setVisible(true);
			}
		});
	}
	
}
